<?
include 'db_info.php';
include 'alert.php'; // includes alert window control functions


// get data from 'make.php' by 'POST' method
$teamname = $_GET['teamname'];
$newteamname = $_POST['newteamname'];
$teampw = $_POST['teampw'];
$newteampw = $_POST['newteampw'];
$newteampw2 = $_POST['newteampw2'];
$vocal = $_POST['vocal'];
$first = $_POST['first'];
$second = $_POST['second'];
$bass = $_POST['bass'];
$drum = $_POST['drum'];
$keyboard = $_POST['keyboard'];
$others = $_POST['others'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>팀 생성 - 트러스 합주시간표</title>
<style type="text/css">
body { color: black; font-size: 10pt; font-family: 맑은고딕, 돋움;}
th { color: white; padding: 10px; font-size: 15pt; font-family: 맑은고딕, 돋움; font-weight: bold; background: #71A7F0;}
td { padding: 5px; }
a { color: #FF4848; font-weight: bold; }
</style>
</head>
<body>

<?
// get row of team
$query = mysqli_query($connect, "SELECT * FROM team WHERE teamname='".$teamname."'");
$team = mysqli_fetch_array($query);
//get row of time
$query = mysqli_query($connect, "SELECT * FROM team_time WHERE teamname='".$teamname."'");
$time = mysqli_fetch_array($query);
mysqli_free_result($query);

// basic error check start
if ($newteamname == "") Alert_back("합주팀 이름을 적어주세요");
if ( strpos($newteamname, "\"") || strpos($newteamname, "'")) Alert_back("팀 이름에 따옴표를 포함할 수 없습니다");

// if $teampw matches to database or admin password (==authority), then break out. Or it turns back
switch ($teampw) {
	case $team['teampw']: 
	case 'authority': if ($newteampw != $newteampw2) Alert_back("변경할 비밀번호가 일치하지 않습니다"); break;
	default: Alert_back("잘못된 비밀번호입니다");
}

if ($newteamname != $team['teamname']) {
	$teamname_check = mysqli_query($connect, "SELECT * FROM team WHERE teamname='$newteamname'");
	if ( mysqli_num_rows($teamname_check) != 0 ) {
		mysqli_free_result($teamname_check);
		Alert_back("이미 사용중인 팀이름입니다");
	}
}

if ($team['isTemp']) Alert_index("임시팀은 수정할 수 없습니다"); // already removed the link from search.php
// basic error check end

// day of week
$week = array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
$week_korean = array("월요일", "화요일", "수요일", "목요일", "금요일", "토요일", "일요일");
// store time of each day (starts from monday)
$start = array();
$end = array();

// count the number of success
$success_counter = 0;

for ($i = 0; $i < 7; $i++) {
	// make string for $_POST index of start time
	$start_string = $week[$i]."_start";
	// store start time of each day
	$start[$i] = $_POST["$start_string"];

	// make string for $_POST index of end time
	$end_string = $week[$i]."_end";
	// store start time of each day
	$end[$i] = $_POST["$end_string"];

	// no input, no error, no insert
	if ($start[$i] == 0 && $end[$i] == 0) continue;

	// basic time error
	if ( $start[$i] > $end[$i] ) { Alert_back( "$week_korean[$i]: 시작/종료 시각 입력 에러" ); }

	// time duplication error (if you are confused, draw time line)
	$query_string = "SELECT * FROM team_time WHERE ($start[$i] <= $start_string && $start_string < $end[$i]) 
												or ($start_string <= $start[$i] && $start[$i] < $end_string)";
	$query = mysqli_query($connect, $query_string);
	while ($row = mysqli_fetch_array($query)) {
		if ($row['teamname'] == $teamname) continue; // escape!
		Alert_back ("시간이 겹치는 팀이 있습니다");
	}
	mysqli_free_result($query);
}

// start data update
// deletion from and rewrite to timetable
for ($i = 0; $i < 7; $i++) { // in 'team_time' table of db, first column is id, second column is team name.
	// deletion
	if ($time[2*$i +2]) { // if any value in '2n'th column of '$time' array
		for ($counter = $time[2*$i +2]; /* start time of the day */ $counter < $time[2*$i +3]; /* start < end */ $counter += 50) {
			$query_string = "UPDATE timetable SET `$week[$i]`='' WHERE `time`=$counter";
			$query = mysqli_query($connect, $query_string);
		}
	}

	// rewrite
	for ($counter = $start[$i]; $counter < $end[$i]; $counter += 50) {
		$query_string = "UPDATE timetable SET $week[$i]='$newteamname' WHERE time='$counter'";
		$query = mysqli_query($connect, $query_string);
	}
}

// team data update
$team_update = mysqli_query($connect, "UPDATE team SET teamname='$newteamname', vocal='$vocal', first='$first', second='$second',
													bass='$bass', drum='$drum', keyboard='$keyboard', others='$others' 
													WHERE teamname='$teamname'");
$time_update = mysqli_query($connect, "UPDATE team_time SET teamname='$newteamname', 
															monday_start='$start[0]', monday_end='$end[0]',
															tuesday_start='$start[1]', tuesday_end='$end[1]',
															wednesday_start='$start[2]', wednesday_start='$end[2]',
															thursday_start='$start[3]', thursday_end='$end[3]',
															friday_start='$start[4]', friday_end='$end[4]',
															saturday_start='$start[5]', saturday_end='$end[5]',
															sunday_start='$start[6]', sunday_end='$end[6]' 
															WHERE teamname='$teamname'");

Alert_index("합주팀 수정 완료");
?>

</body>
</html>